package com.graduation.springbootinit.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduation.springbootinit.model.entity.ShopPart.MonthlyStoreSales;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface MonthlyStoreSalesMapper extends BaseMapper<MonthlyStoreSales> {
    
    /**
     * 获取指定年月的商铺销售数据
     */
    @Select("SELECT * FROM shop_part_monthly_store_sales WHERE year = #{year} AND LPAD(month, 2, '0') = LPAD(#{month}, 2, '0')")
    List<MonthlyStoreSales> getMonthlyStoreSales(@Param("year") Integer year, @Param("month") Integer month);
    
    /**
     * 获取指定商铺的销售数据（分页）
     */
    @Select("SELECT * FROM shop_part_monthly_store_sales WHERE store_id = #{storeId} ORDER BY year DESC, month DESC")
    Page<MonthlyStoreSales> getStoreSalesHistory(Page<MonthlyStoreSales> page, @Param("storeId") Long storeId);
    
    /**
     * 获取指定年月的销售总额
     */
    @Select("SELECT SUM(total_sales) FROM shop_part_monthly_store_sales WHERE year = #{year} AND LPAD(month, 2, '0') = LPAD(#{month}, 2, '0')")
    BigDecimal getTotalSalesByMonth(@Param("year") Integer year, @Param("month") Integer month);
    
    /**
     * 获取指定年月的平台佣金总额
     */
    @Select("SELECT SUM(commission_amount) FROM shop_part_monthly_store_sales WHERE year = #{year} AND LPAD(month, 2, '0') = LPAD(#{month}, 2, '0')")
    BigDecimal getTotalCommissionByMonth(@Param("year") Integer year, @Param("month") Integer month);
    
    /**
     * 获取指定年份的月度销售数据
     */
    @Select("SELECT * FROM shop_part_monthly_store_sales WHERE year = #{year} ORDER BY month ASC")
    List<MonthlyStoreSales> getYearlyStoreSales(@Param("year") Integer year);
    
    /**
     * 获取指定商铺在指定年月的销售数据
     */
    @Select("SELECT * FROM shop_part_monthly_store_sales WHERE store_id = #{storeId} AND year = #{year} AND LPAD(month, 2, '0') = LPAD(#{month}, 2, '0')")
    MonthlyStoreSales getStoreMonthlySales(@Param("storeId") Long storeId, @Param("year") Integer year, @Param("month") Integer month);
}